<?php
//Класс
class TypeCatalog implements Collection {

    private $name;
    private $id;
    private $children;
    private $parentId;
    private $access;
    private $publish;
    private $isSubTypes;
    private $template;
    private $attributes;

    public function __construct($name, $id, $isSubTypes = false, $template = false, $parentId = false, $access = false, $publish = false, $attributes = false) {
        $this->name = $name;
        $this->id = $id;
        if ($parentId) {
            $this->parentId = $parentId;
        }
        if ($access) {
            $this->access = $access;
        }
        if ($publish) {
            $this->publish = $publish;
        }
        if ($isSubTypes) {
            $this->isSubTypes = $isSubTypes;
        }
        if ($template) {
            $this->template = $template;
        }
        if ($attributes) {
            $this->attributes = $attributes;
        }
    }

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    //методы реализуюшие методы обявленые в интерфейсе Collection
    public function add(Collection $element) {
        //заглушка
        //$this->elements[] = $element;
    }

    public function getChildren() {
        if ($this->children == null) {
            $query = 'SELECT * FROM `' . DBPREFIX . 'types` WHERE `pid` = ' . $this->id;
            $result = mysql_query($query) or die('Ошибка в запросе' . $query . ' ' . mysql_error());
            $num = mysql_num_rows($result);
            if ($num > 0) {
                while ($row = mysql_fetch_assoc($result)) {
                    $type[] = new TypeCatalog($row['type'], $row['id'], $row['isSubTypes'], false, $row['pid']);
                }
                $this->elements = $type;
                return $type;
            } else {
                return null;
            }
        } else {
            return $this->children;
        }
    }

    public function getItemList($start, $number) {
        $query = 'SELECT * FROM `' . DBPREFIX . 'itemCatalog` WHERE `idCatalog` = ' . $this->id . ' ORDER BY `addDate` ASC LIMIT ' . $start . ', ' . $number;
        $result = mysql_query($query) or die('Ошибка в запросе' . $query . ' ' . mysql_error());
        $num = mysql_num_rows($result);
        if ($num > 0) {
            while ($row = mysql_fetch_assoc($result)) {
                $items[] = new ItemCatalog($row['id'], $row['name'], $row['idCatalog'], null, $row['description']);
                //return $
            }
            return $items;
        } else {
            return false;
        }
    }

    public function getItems() {
        $query = 'SELECT * FROM `' . DBPREFIX . 'itemCatalog` WHERE `idCatalog` = ' . $this->id;
        $result = mysql_query($query) or die('Ошибка в запросе' . $query . ' ' . mysql_error());
        $num = mysql_num_rows($result);
        if ($num > 0) {
            while ($row = mysql_fetch_assoc($result)) {
                $items[] = new ItemCatalog($row['id'], $row['name'], $row['idCatalog'], null, $row['description']);
                //return $
            }
            return $items;
        } else {
            return false;
        }
    }

    //метод возврашает наименование по его обьекту
    /* public function getItem($itemId, $isAdmin = false) {
      $query = 'SELECT * FROM `' . DBPREFIX . 'itemCatalog` WHERE `id` = ' . $itemId . ' AND `idCatalog` = ' . $this->id;
      $result = mysql_query($query) or die('Ошибка в запросе' . $query . ' ' . mysql_error());
      if($row =  mysql_fetch_assoc($result)) {
      $item = new ItemCatalog($row['id'], $row['name'], $row['idCatalog'], null, $row['description'], $row['title']);
      return $item;
      }
      else {
      return false;
      }
      } */
    public function getItem($itemId, $isAdmin = false) {
        $query = 'SELECT * FROM `' . DBPREFIX . 'itemCatalog` WHERE `id` = ' . $itemId . ' AND `idCatalog` = ' . $this->id;
        $result = mysql_query($query) or die('Ошибка в запросе' . $query . ' ' . mysql_error());
        if ($row = mysql_fetch_assoc($result)) {
            $item = new ItemCatalog($row['id'], $row['name'], $row['idCatalog'], null, $row['description'], $row['title'], $row['descriptionStr'], $row['keywordsStr']);
            //var_dump($row);
            return $item;
        } else {
            return false;
        }
    }

    public function insert() {
        $query = 'INSERT INTO `' . DBPREFIX . 'types` (`type`, `access`, `pid`, `moduleid`, `publish`, `isSubTypes`)
					VALUES ("' . $this->name . '",
							"' . $this->access . '",
							"' . $this->parentId . '",
							"4",
							"' . $this->publish . '",
							"' . $this->isSubTypes . '")';
        //echo $query;
        $result = mysql_query($query) or die('Ошибка в запросе' . $query . ' ' . mysql_error());
        $idType = mysql_insert_id();
        CatalogAdminModel::insertAttributesCatalog($this->attributes, $idType);
        CatalogAdminModel::createAdditionalCatalogItem($this->attributes, $idType);
        $query = 'INSERT INTO `' . DBPREFIX . 'templateCatalog` (`idType`, `name`)
					VALUES ("' . $idType . '",
							"' . $this->template . '")';
        $result = mysql_query($query) or die('Ошибка в запросе' . $query . ' ' . mysql_error());
        return $idType;
    }

    public function getTemplate() {
        $query = 'SELECT * FROM `' . DBPREFIX . 'templateCatalog` WHERE idType = ' . $this->id;
        $result = mysql_query($query) or die('Ошибка в запросе' . $query . ' ' . mysql_error());
        $num = mysql_num_rows($result);
        if ($num > 0) {
            $row = mysql_fetch_assoc($result);
            return $row['name'];
        } else {
            return 'default.php';
        }
    }

    public function isSubTypes() {
        return $this->isSubTypes;
    }

    public function update() {
        //echo 1;
        //exit;
        $query = 'UPDATE `' . DBPREFIX . 'types` SET `type` = ? WHERE `id` = ?';
        $query = mysql_make_qw($query, $this->name, $this->id);
        $result = mysql_query($query) or die('Ошибка в запросе' . $query . ' ' . mysql_error());
    }

    public function getNumItem() {
        $num = 0;
        if ($this->isSubTypes()) {
            //ob_start();
            //echo 1;
            $children = $this->getChildren();
            if ($children) {
                foreach ($children as $element) {
                    $num = $num + $element->getNumItem();
                }
            }
        } else {
            $query = 'SELECT count(`id`) num FROM `' . DBPREFIX . 'itemCatalog` WHERE `idCatalog` = ?';
            $query = $query = mysql_make_qw($query, $this->id);
            $result = mysql_query($query) or die('Ощибка в запросе ' . $query . ' ' . mysql_error());
            if ($row = mysql_fetch_assoc($result)) {
                $num = $num + $row['num'];
            }
        }
        return $num;
    }
	
	public function eugeneMethod()
	{
	}

    public function getParentId() {
        return $this->parentId;
    }

}

?>